import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * Description:InputStream 的使用
 * Author: WJW
 * Date: 2024-08-20
 * Time: 21:58
 */
public class IODemo2 {
    public static void main(String[] args) {
        try(InputStream inputStream = new FileInputStream("./test.txt")){
            while (true){
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1){
                    break;
                }
                String s = new String(buffer,0,n);//此处构造String是通过前n个字节构造而不是整个数组，实际读取文件的内容可能不足1024
                System.out.println(s);
            }
        }  catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
